package com.sojson.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.ConfigParamVerify;
import com.sojson.entity.po.Config;
import com.sojson.enums.core.EConfigKey;
import com.sojson.result.Result;
import com.sojson.service.ConfigService;
import com.sojson.util.StringUtil;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;

/**
 * 配置表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "配置表管理")
@RestController
@RequestMapping(value = "/config")
public class ConfigController {

    @Resource
    private ConfigService configServiceImpl;

    /**
     * 单条插入
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Object insert(@RequestBody Config config) throws SQLException {
        configServiceImpl.insert(config);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody Config config) throws SQLException {
        return Result.success(configServiceImpl.insertList(config));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody Config config) throws SQLException {
        return Result.success(configServiceImpl.deleteByDto(config));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() throws SQLException {
        int countAll = configServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() throws SQLException {
        List<Config> findAll = configServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countByDto", method = RequestMethod.POST)
    public Object countByDto(@RequestBody Config config) throws SQLException {
        int countByDto = configServiceImpl.countByDto(config);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    @GetMapping("/findByDto")
    @PreAuthorize("@ss.hasPermi('/config/findByDto')")
    public Object findByDto(@RequestBody Config config) throws SQLException {
        List<Config> findByDto = configServiceImpl.findByDto(config);
        return Result.success(findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Object deleteById(Integer id) throws SQLException {
        configServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    public Object deleteByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        return Result.success(configServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody Config config) throws SQLException {
        configServiceImpl.updateById(config);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody Config config) throws SQLException {
        return Result.success(configServiceImpl.updateStaticByIdList(config));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody Config config) throws SQLException {
        return Result.success(configServiceImpl.updateChangeByIdList(config));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updatePartialById", method = RequestMethod.POST)
    public Object updatePartialById(@RequestBody Config config) throws SQLException {
        configServiceImpl.updatePartialById(config);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody Config config) throws SQLException {
        return Result.success(configServiceImpl.updateStaticPartialByIdList(config));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody Config config) throws SQLException {
        return Result.success(configServiceImpl.updateChangePartialByIdList(config));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Object findById(Integer id) throws SQLException {
        Config findById = configServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<Config> findByIdList = configServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param config
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody Config config) throws SQLException {
        ArrayList<Integer> findIdByDto = configServiceImpl.findIdByDto(config);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() throws SQLException {
        configServiceImpl.clear();
        return Result.success();
    }

    /**
     * 通过别名查询
     * 
     * @param conAlias
     * @return
     */
    @GetMapping("/findByConAlias")
    @PreAuthorize("@ss.hasPermi('/config/findByConAlias')")
    public Object findByConAlias(String conAlias) {
        Config findByConAlias = configServiceImpl.findByConAlias(conAlias);
        return Result.success(findByConAlias);
    }

    /**
     * 查询诚意金
     * 
     * @param conAlias
     * @return
     * @throws IOException 
     */
    // @GetMapping("/findDeposit")
    public Object findDeposit() throws IOException {
        Map<String, Object> hashMap = new HashMap<>();
        Config findByConAlias = configServiceImpl.findByConAlias(EConfigKey.DEPOSIT.getKey());
        hashMap.put("deposit", findByConAlias);
        hashMap.put("token", TokenUtil.refreshTokenUser());
        return Result.success(hashMap);
    }

    /**
     * 通过别名更新(局部更新)
     * 
     * @param config
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @PutMapping("/updatePartialByConAlias")
    @PreAuthorize("@ss.hasPermi('/config/updatePartialByConAlias')")
    public Object updatePartialByConAlias(@RequestBody Config config) throws SQLException, IOException {
        // 校验参数
        Result<?> paramResult = ConfigParamVerify.updatePartialByConAlias(config);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        config.updateInit();
        return Result.success(configServiceImpl.updatePartialByConAlias(config));
    }

    /**
     * 查询推广海报背景图
     * 
     * @return
     */
    @GetMapping("/findImgBgPromotionPoster")
    public Object findImgBgPromotionPoster() {
        Config findByConAlias = configServiceImpl.findByConAlias(EConfigKey.IMG_BG_PROMOTION_POSTER.getKey());
        return Result.success(StringUtil.isBlankObject(findByConAlias) ? null : findByConAlias.getVal());
    }

    /**
     * 修改推广海报背景图
     * 
     * @return
     * @throws IOException 
     */
    @PutMapping("/updateImgBgPromotionPoster")
    @PreAuthorize("@ss.hasPermi('/config/updatePartialByConAlias')")
    public Object updateImgBgPromotionPoster(String file) throws IOException {
        Config config = new Config();
        config.updateInit();
        config.setConAlias(EConfigKey.IMG_BG_PROMOTION_POSTER.getKey());
        config.setVal(file);
        return Result.success(configServiceImpl.updateImgByConAlias(config));
    }

}